//
//  ABLabel.m
//  Autobots
//
//  Created by 张志恒 on 16/10/20.
//  Copyright © 2016年 Autohome. All rights reserved.
//

#import "RTPlaceholderLabel.h"

@interface RTPlaceholderLabel ()

@property (nonatomic, strong) UILabel *placeholderLabel;

@end

@implementation RTPlaceholderLabel

- (instancetype)init
{
    return [self initWithFrame:CGRectZero];
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self commonInit];
    }
    return self;
}

- (instancetype)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self) {
        [self commonInit];
    }
    return self;
}

- (void)commonInit
{
    _placeholderLabel = [[UILabel alloc] init];
    _placeholderLabel.textColor = [UIColor colorWithRed:199/255.f green:199/255.f blue:205/255.f alpha:1];
    _placeholderLabel.backgroundColor = [UIColor clearColor];
    _placeholderLabel.opaque = NO;
    _placeholderLabel.translatesAutoresizingMaskIntoConstraints = NO;
    [self addSubview:_placeholderLabel];
    [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[_placeholderLabel]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_placeholderLabel)]];
    [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[_placeholderLabel]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_placeholderLabel)]];
}

- (NSString *)placeholder
{
    return self.placeholderLabel.text;
}

- (void)setPlaceholder:(NSString *)placeholder
{
    self.placeholderLabel.text = placeholder;
}

- (UIColor *)placeholderColor
{
    return self.placeholderLabel.textColor;
}

- (void)setPlaceholderColor:(UIColor *)placeholderColor
{
    self.placeholderLabel.textColor = placeholderColor;
}

#pragma mark - override
- (void)setText:(NSString *)text
{
    [super setText:text];
    self.placeholderLabel.hidden = text ? YES : NO;
}

- (void)setAttributedText:(NSAttributedString *)attributedText
{
    [super setAttributedText:attributedText];
    self.placeholderLabel.hidden = attributedText ? YES : NO;
}

- (void)setTextAlignment:(NSTextAlignment)textAlignment
{
    [super setTextAlignment:textAlignment];
    self.placeholderLabel.textAlignment = textAlignment;
}

- (void)setFont:(UIFont *)font
{
    [super setFont:font];
    self.placeholderLabel.font = font;
}

- (void)setLineBreakMode:(NSLineBreakMode)lineBreakMode
{
    [super setLineBreakMode:lineBreakMode];
    self.placeholderLabel.lineBreakMode = lineBreakMode;
}

- (void)setNumberOfLines:(NSInteger)numberOfLines
{
    [super setNumberOfLines:numberOfLines];
    self.placeholderLabel.numberOfLines = numberOfLines;
}

@end
